/**
 * 
 */
package com.googlecode.bestpaper.syntax;

import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.BadElementException;

/**
 * @author xalinx at gmail dot com
 * @date Dec 2, 2009
 */
public abstract class BPFatherMarkup<I, O> implements BPMarkup<I> {
	private List<BPMarkup> children;

	private String title;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	private int titleFontSize = 12;

	public int getTitleFontSize() {
		return titleFontSize;
	}

	public void setTitleFontSize(int titleFontSize) {
		this.titleFontSize = titleFontSize;
	}

	private String content;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public BPFatherMarkup(String title, String content, int titleFontSize) {
		this(title, content);
		this.titleFontSize = titleFontSize;
	}
	
	public BPFatherMarkup(String title, String content) {
		this.title = title;
		this.content = content;
	}

	/**
	 * 
	 */
	public BPFatherMarkup() {
	}

	public <E extends BPMarkup> void  addChild(E child) {
		if (this.children == null) {
			this.children = new ArrayList<BPMarkup>();
		}
		this.children.add(child);
	}
	
	public List<BPMarkup> getChildren(){
		return this.children;
	}
	
	public BPMarkup getChild(int index){
		return this.children.get(index);
	}

	public void buildComponents(O writer, int fontStyle) throws Exception {
		if (null == children) {
			return;
		}
		for (BPMarkup<O> c : children) {
			c.setTitleFontSize(this.getTitleFontSize() - 4);
			c.build(writer, fontStyle);
		}
	}

	protected O buildSelf(I writer, int fontStyle) throws Exception {
		return null;
	}
	
	protected void finishBuild(I writer, O out) throws Exception{
		
	}

	@Override
	public void build(I writer, int fontStyle) throws Exception {
		O mine = this.buildSelf(writer, fontStyle);
		this.buildComponents(mine, fontStyle);
		this.finishBuild(writer, mine);
	}

}
